/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */

using System;
using System.Xml;
using System.Net;
using System.IO;
using System.Configuration;

namespace Pbdms.BusinessLogic.RSS
{
	/// <summary>
	/// XmlUtils contains various XML related functions, plus the general Web function createWebRequest.
	/// </summary>
	public class XmlUtils
	{
		

		public static bool useProxy = false;
		public static WebProxy webProxySetting = null;
		public static int timeOutMilliseconds = 60000;
		public static NetworkCredential proxyCredentials = null;

		/// <summary>
		/// Loads an XML document from the Uri.
		/// </summary>
		/// <param name="uri"></param>
		/// <returns></returns>
		public static XmlDocument loadXml(string filename) {
			XmlDocument document = new XmlDocument();
		

		    document.Load(filename);
	
			return document;
		}

		
		/// <summary>
		/// Saves an xml document to the uri. 
		/// NB: Only files are supported.
		/// </summary>
		/// <param name="document"></param>
		/// <param name="uri"></param>
		public static void saveXml(XmlDocument document, System.Uri uri) {
			if (uri.IsFile) {
				document.Save(uri.LocalPath);
			} else {
				throw new NotSupportedException("Saving to " + uri + " is not supported at the moment");
			}
		}

		/// <summary>
		/// Get the text of a node, according to root, xpath
		/// Uses a namespace manager to lookup prefixes
		/// </summary>
		/// <param name="root"></param>
		/// <param name="xpath"></param>
		/// <param name="nsmgr"></param>
		/// <returns></returns>
		public static string getNodeText(XmlNode root, string xpath, XmlNamespaceManager nsmgr) {
			XmlNode node = root.SelectSingleNode(xpath, nsmgr);
			if (node == null) {
				
				return "";
			} else {
				return node.InnerText;
			}
		}

		/// <summary>
		/// Get the text of a node, according to root, xpath
		/// </summary>
		/// <param name="root"></param>
		/// <param name="xpath"></param>
		/// <returns></returns>
		public static string getNodeText(XmlNode root, string xpath) {
			XmlNode node = root.SelectSingleNode(xpath);
			if (node == null) {
				
				return "";
			} else {
				return node.InnerText;
			}
		}


		/// <summary>
		/// Get the DateTime value of a node, according to root, xpath. If it fails, you get the DateTime.Now
		/// No exception is thrown, since it wasn't needed for this purpose.
		/// Uses a namespace manager to lookup prefixes
		/// </summary>
		/// <param name="root"></param>
		/// <param name="xpath"></param>
		/// <param name="nsmgr"></param>
		/// <returns></returns>
		public static DateTime getNodeDateTime(XmlNode root, string xpath, XmlNamespaceManager nsmgr) {
			string text = getNodeText(root, xpath, nsmgr);
			DateTime dateTime = DateTime.Now;
			try {
				dateTime = DateTime.Parse(text);
			} catch {
				
			}
			return dateTime;
		}

		/// <summary>
		/// Get the DateTime value of a node, according to root, xpath. If it fails, you get the DateTime.Now
		/// No exception is thrown, since it wasn't needed for this purpose.
		/// </summary>
		/// <param name="root"></param>
		/// <param name="xpath"></param>
		/// <returns></returns>
		public static DateTime getNodeDateTime(XmlNode root, string xpath) {
			string text = getNodeText(root, xpath);
			DateTime dateTime = DateTime.Now;
			try {
				dateTime = DateTime.Parse(text);
			} catch  {

			}
			return dateTime;
		}

		
		/// <summary>
		/// Get the integer value of a node, according to root, xpath. If it fails, you get 0
		/// No exception is thrown, since it wasn't needed for this purpose.
		/// Uses a namespace manager to lookup prefixes
		/// </summary>
		/// <param name="root"></param>
		/// <param name="xpath"></param>
		/// <param name="nsmgr"></param>
		/// <returns></returns>
		public static int getNodeIntegerValue(XmlNode root, string xpath, XmlNamespaceManager nsmgr) {
			string text = getNodeText(root, xpath, nsmgr);
			int returnValue = 0;
			try {
				returnValue = Int32.Parse(text);
			} catch (Exception e) {
				
			}
			return returnValue;
		}

		/// <summary>
		/// Get the integer value of a node, according to root, xpath. If it fails, you get 0
		/// No exception is thrown, since it wasn't needed for this purpose.
		/// </summary>
		/// <param name="root"></param>
		/// <param name="xpath"></param>
		/// <returns></returns>
		public static int getNodeIntegerValue(XmlNode root, string xpath) {
			string text = getNodeText(root, xpath);
			int returnValue = 0;
			try {
				returnValue = Int32.Parse(text);
			} catch (Exception e) {
				
			}
			return returnValue;
		}

		/// <summary>
		/// Gets the attribute value (as string) for a node.
		/// </summary>
		/// <param name="node">Node</param>
		/// <param name="attributeName">attribute name</param>
		/// <param name="namespaceUri">attribute namespace</param>
		/// <returns></returns>
		public static string getAttributeValue(XmlNode node, string attributeName, string namespaceUri) {
			if (node == null) {
				
				return "";
			}

			XmlNode xmlNode = node.Attributes.GetNamedItem(attributeName, namespaceUri);
			if (xmlNode == null) {
				
				return "";
			} else {
				return xmlNode.InnerText;
			}
		}
		
		/// <summary>
		/// Gets the attribute bool value for a node.
		/// </summary>
		/// <param name="node">Node</param>
		/// <param name="attributeName">attribute name</param>
		/// <param name="namespaceUri">attribute namespace</param>
		/// <returns>bool value. False if the operation fails</returns>
		public static bool getAttributeBoolValue(XmlNode node, string attributeName, string namespaceUri) {
			if (node == null) {
				
			}

			XmlNode xmlNode = node.Attributes.GetNamedItem(attributeName, namespaceUri);
			if (xmlNode == null) {
				
				return false;
			} else {
				return Boolean.Parse(xmlNode.InnerText);
			}
		}

		/// <summary>
		/// Adds an attribute to a node.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="attrName"></param>
		/// <param name="attrValue"></param>
		public static void addAttribute(XmlNode node, string attrName, string attrValue) {
			XmlAttribute urlAttr = node.OwnerDocument.CreateAttribute(attrName);
			urlAttr.InnerText = attrValue;
			node.Attributes.SetNamedItem(urlAttr);
		}

		/// <summary>
		/// Adds a new text node to the node specified.
		/// </summary>
		/// <param name="node">The node which to append the new node to</param>
		/// <param name="nodeName">The name of the new node</param>
		/// <param name="nodeValue">The value of the new node</param>
		public static XmlElement addTextNode(XmlNode node, string nodeName, string nodeValue) {
			XmlElement newNode = addNode(node, nodeName);
			newNode.InnerText = nodeValue;
			return newNode;
		}

		/// <summary>
		/// Adds a new node with name of the nodeName variable to the node supplied
		/// </summary>
		/// <param name="node">The node with the new node is appended to</param>
		/// <param name="nodeName">The name of the new node</param>
		public static XmlElement addNode(XmlNode node, string nodeName) {
			XmlElement newNode = node.OwnerDocument.CreateElement(nodeName);
			node.AppendChild(newNode);
			return newNode;
		}

		/// <summary>
		/// Adds a new node to the document at the location specified by the xpath statement
		/// </summary>
		/// <param name="document">The document which to add the node</param>
		/// <param name="xpath">The XPath statement which must return a node which the node node is appended to</param>
		/// <param name="nodeName">The name of the new node</param>
		public static XmlElement addNode(XmlDocument document, string xpath, string nodeName) {
			XmlNode nodeToAppendTo = document.SelectSingleNode(xpath);
			if (nodeToAppendTo == null) {
				throw new Exception("XPath query didn't result in a node");
			}
			return addNode(nodeToAppendTo, nodeName);
		}
	}
}
